package Lab3;

import java.util.EmptyStackException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * Test of the Lab3 class Stack.
 *
 * @author dale
 */
public class StackTest {
    private Integer one     = new Integer(1);
    private Integer two     = new Integer(2);
    private Integer three   = new Integer(3);
    private Integer four    = new Integer(4);

    public StackTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of empty method, of class Stack.
     */
    @Test
    public void testEmpty() {
        System.out.println("empty");
        Stack<Integer> instance = new Stack<Integer>();

        // Positive test
        boolean result = instance.empty();
        assertEquals(true, result);
        
        // Negative test
        instance.push(one);
        result = instance.empty();
        assertEquals(false, result);
    }

    /**
     * Test of peek method, of class Stack.
     */
    @Test
    public void testPeek() {
        System.out.println("peek");
        Stack<Integer> instance = new Stack<Integer>();
        
        // Exception Test
        try {
            Integer result = instance.peek();
            fail("Should have thrown exception with empty stack");
        }
        catch (EmptyStackException ex) {
            // No action needed here
        }

        // Positive Test
        instance.push(one);
        instance.push(two);
        Integer expResult = two;
        Integer result = instance.peek();
        assertEquals(expResult, result);

        // Negative Test
        expResult = three;
        result = instance.peek();
        assertNotSame(expResult, result);
    }

    /**
     * Test of pop method, of class Stack.
     */
    @Test
    public void testPop() {
        System.out.println("pop");
        Stack<Integer> instance = new Stack<Integer>();

        // Positive Test
        instance.push(one);
        instance.push(two);
        instance.push(three);
        Integer expResult = three;
        Integer result = instance.pop();
        assertEquals(expResult, result);
        expResult = two;
        result = instance.pop();
        assertEquals(expResult, result);

        // Exception Test
        result = instance.pop();  // Empty stack
        try {
            result = instance.pop();
            fail("Should have thrown exception with empty stack");
        }
        catch (EmptyStackException ex) {
            // No action needed here
        }

    }

    /**
     * Test of push method, of class Stack.
     */
    @Test
    public void testPush() {
        System.out.println("push");
        Stack<Integer> instance = new Stack<Integer>();
        Integer expResult = four;
        Integer result = instance.push(four);
        Integer result2 = instance.peek();
        assertEquals(expResult, result);
        assertEquals(expResult, result2);
    }

    /**
     * Test of search method, of class Stack.
     */
    @Test
    public void testSearch() {
        System.out.println("search");
        Stack<Integer> instance = new Stack<Integer>();

        // Positive Test
        instance.push(one);
        instance.push(two);
        instance.push(three);
        int expResult = 1;
        int result = instance.search(three);
        assertEquals(expResult, result);

        // Negative Test
        expResult = -1;
        result = instance.search(four);
        assertEquals(expResult, result);
    }

    @Test
    public void testSearchLong() {
    	System.out.println("Long Search");
    	Stack<Integer> instance = new Stack<Integer>();
    	Integer five = new Integer(5);
    	Integer six = new Integer(6);
    	Integer seven = new Integer(7);
    	Integer eight = new Integer(8);
    	
    	int expResult;
    	int result;
    	
    	instance.push(one);
    	instance.push(two);
    	instance.push(three);
    	instance.push(four);
    	instance.push(five);
    	instance.push(six);
    	instance.push(seven);
    	instance.push(eight);
    	
    	expResult = 6;
    	result = instance.search(three);
    	assertEquals(expResult, result);
    	
    	expResult = 3;
    	result = instance.search(six);
    	assertEquals(expResult, result);
    	
    	expResult = 8;
    	result = instance.search(one);
    	assertEquals(expResult, result);
    }
    
    @Test
    public void searchEmptyList() {
    	Stack<Integer> instance = new Stack<Integer>();
    	
    	assertTrue(instance.empty());
    	
    	try {
    		instance.search(one);
    		fail("Should be checking for empty stacks!");
    	} catch (EmptyStackException ex) {
    		// doin' nothin'.
    	}
    }
}